var loginName = document.getElementById("loginName");
var loginPwd = document.getElementById("loginPwd");
var access_token = "";
var backButtonPress = 0;
(function($) {
	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	clientWidth = document.documentElement.clientWidth;
	setPxPerRem(clientWidth);
	mui.plusReady(function() {

		plus.navigator.setStatusBarBackground('#FFFFFF');
		mui.back = function(event) {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast(tuichu);
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};

	});
})(mui);
document.getElementById("visitorLogin").addEventListener('tap', function() {
	localStorage.removeItem("pid");
	localStorage.removeItem("isLogin");
	plus.runtime.quit();
}, false);

document.getElementById("loginBtn").addEventListener('click', function() {
	if(isHaveNet()) {
		getToken();
	} else {
		mui.toast("无网络，请确认网络连接！")
	}
}, false);

//获取原始窗口的高度
var originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
window.onresize = function() {

	//软键盘弹起与隐藏  都会引起窗口的高度发生变化
	var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;

	if(resizeHeight * 1 < originalHeight * 1) { //resizeHeight<originalHeight证明窗口被挤压了

		plus.webview.currentWebview().setStyle({
			height: originalHeight
		});
	}
}

//登录
function getToken() {

	var username = loginName.value.trim();
	var passWord = loginPwd.value.trim();
	//测试用
//		username = 'admin';
//		passWord = '123456'
	if(isEmpty(username)) {
		mui.toast("请输入用户名");
		return;
	}

	if(isEmpty(passWord)) {
		mui.toast("请输入密码");
		return;
	}

	var pJson = {
		name: username,
		pwd: passWord
	}

	var pstr = JSON.stringify(pJson);
	var jsonData = {
		op: "PhoneLogin",
		pjson: pstr
	}

	if(checkParams(jsonData)) {
		mui.toast("请求失败,请稍后再试");
		return;
	}
	var opts = {
		url: '',
		isAllData: true,
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	};
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		console.log(JSON.stringify(data));

		if(!isEmpty(data)) {

			if(isEmpty(data.status) || data.status != 1) {
				mui.toast("登陆验证失败，请稍后再试");
				return;
			}
			var isKehu = false ; 
			if(!isEmpty(data.Unit)){
				localStorage.setItem('isKehu', data.Unit);
			}
			
			localStorage.setItem('pid', data.Pid);
			localStorage.setItem("ueserName" , data.Pname);
			localStorage.setItem('isLogin', true);

			var postData = {
				pageflag: "page_login.html"
			}
			var login = {
				pageUrl: "page_index.html",
				postData: postData,
				aniShow: "fade-in"
			};
			pageChange(login);
		} else {
			mui.toast("登陆验证失败，请稍后再试");
		}
	});

}